home *** CD-ROM | disk | FTP | other *** search
/ Amiga Tools 5 / Amiga Tools 5.iso / tools / developer-tools / c-tools / c_examples / slider / led.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1996-05-23  |  3.5 KB  |  118 lines

  1. //////////////////////////////////////////////////////////////////////////////
  2. // Led.cpp
  3. //
  4. // Deryk Robosson
  5. // April 25, 1996
  6. //////////////////////////////////////////////////////////////////////////////
  7.  
  8. //////////////////////////////////////////////////////////////////////////////
  9. // INCLUDES
  10. #include "aframe:include/Led.hpp"
  11.  
  12. //////////////////////////////////////////////////////////////////////////////
  13. //
  14.  
  15. // Open led.image if it's not already open =)
  16. AFLed::AFLed()
  17. {
  18.     int i;
  19.  
  20.     m_Global.Added=FALSE;
  21.     m_Global.Image=NULL;
  22.     m_Global.Colon=TRUE;
  23.     m_Global.Negative=FALSE;
  24.     m_Global.Signed=TRUE;
  25.     m_Global.FGPen=1;
  26.     m_Global.BGPen=0;
  27.     m_Global.NumPairs=1;
  28.     m_Global.Window=NULL;
  29.     m_Global.Image=NULL;
  30.     LedLibrary=NULL;
  31.  
  32.     for(i=0;i<m_Global.NumPairs;i++)
  33.         m_Global.DigitPairs[i]=0;
  34.  
  35.     if(LedLibrary == NULL)
  36.         if(!(LedLibrary=(struct ClassLibrary*)OpenLibrary((UBYTE*)"images/led.image",(ULONG)37)))
  37.             if(!(LedLibrary=(struct ClassLibrary*)OpenLibrary((UBYTE*)":classes/images/led.image",(ULONG)37)))
  38.                 if(!(LedLibrary=(struct ClassLibrary*)OpenLibrary((UBYTE*)"classes/images/led.image",(ULONG)37)))
  39.                     printf("Failed to open led.image\n");
  40. }
  41.  
  42. AFLed::~AFLed()
  43. {   // Call DestroyObject if it hasn't already
  44.     if(m_Global.Image != NULL)
  45.         DestroyObject();
  46. }
  47.  
  48. // Remove object from window if it has been added
  49. // and dispose of it
  50. void AFLed::DestroyObject()
  51. {
  52.     RemoveObject();
  53.  
  54.     DisposeObject((Object*)m_Global.Image);
  55.     m_Global.Image=NULL;
  56.  
  57.     if(LedLibrary) {
  58.         CloseLibrary((struct Library*)LedLibrary);
  59.         LedLibrary=NULL;
  60.     }
  61. }
  62.  
  63. // Add object to a window, with size and position of rect and gadetid of id
  64. BOOL AFLed::Create(AFWindow *window, AFRect *rect, ULONG id)
  65. {
  66.     if(m_Global.Added)
  67.         RemoveObject();
  68.  
  69.     if(m_Global.Image=(struct Image*)NewObject((struct IClass*)NULL, (UBYTE*)"led.image",
  70.                         IA_FGPEN, m_Global.FGPen,
  71.                         IA_BGPen, m_Global.BGPen,
  72.                         IA_Width, rect->Width(),
  73.                         IA_Height, rect->Height(),
  74.                         LED_Pairs, m_Global.NumPairs,
  75.                         LED_Values, m_Global.DigitPairs,
  76.                         LED_Colon, m_Global.Colon,
  77.                         LED_Negative, m_Global.Negative,
  78.                         LED_Signed, m_Global.Signed,
  79.                         GA_ID, id,
  80.                         ICA_TARGET, ICTARGET_IDCMP,
  81.                         TAG_DONE)) {
  82.         AddGadget(window->m_pWindow, (struct Gadget*)m_Global.Image, -1);
  83.  
  84.         DrawImage(window->m_pWindow->RPort, m_Global.Image, rect->TopLeft()->m_x, rect->TopLeft()->m_y);
  85.  
  86.         m_Global.Window=window;
  87.         m_Global.Added=TRUE;
  88.         m_Global.rect.SetRect(rect->TopLeft(),rect->BottomRight());
  89.         return TRUE;
  90.     } else return FALSE;
  91. }
  92.  
  93. // Remove object from the window to which it was added
  94. LONG AFLed::RemoveObject()
  95. {
  96.     LONG result;
  97.  
  98.     if(m_Global.Image !=NULL) {
  99.         if(m_Global.Added) {
  100.             m_Global.Added=FALSE;
  101.             result=RemoveGadget(m_Global.Window->m_pWindow,(struct Gadget*)m_Global.Image);
  102.             m_Global.Window=NULL;
  103.             return result;
  104.         } else return FALSE;
  105.     } else return FALSE;
  106. }
  107.  
  108. void AFLed::RefreshImage()
  109. {
  110.     DrawImage(m_Global.Window->m_pWindow->RPort, m_Global.Image, m_Global.rect.TopLeft()->m_x, m_Global.rect.TopLeft()->m_y);
  111. }
  112.  
  113. void AFLed::SetDigits(int pair, int number)
  114. {
  115.     m_Global.DigitPairs[pair]=number;
  116.     Create(m_Global.Window, &m_Global.rect, m_Global.id);
  117. }
  118.